<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CouponService extends BaseService
{
    //会员劵

    protected $scences = [
        '全场通用'
    ];
    protected $statuss = [
        '', '未领取', '已领取未使用', '已过期', '已使用',
    ];
    protected $types = [
        1 => '新用户可得', //'充值可得',
    ];

    /**
     * 会员劵列表
     * @param array $pages【start，limit】
     */
    public function getIndex($pages, $uid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbUcoupon);
        if ($uid) {
            $db = $db->where('uid', $uid);
        }
        $total = $db->count();
        $res = $db->orderBy('id', 'desc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有优惠劵');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //用户信息
            $uname = '-';
            $userOne = (array) DB::table($this->dbUser)->where('id', $v['uid'])->first();
            if (!empty($userOne)) {
                $uname = $userOne['nickname'];
            }
            //场景
            $scences = $this->scences;
            $sceneName = array_key_exists($v['scene'], $scences) ? $scences[$v['scene']] : '';
            //状态
            $statuss = $this->statuss;
            $statusName = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '';
            $arys[] = [
                'id' => $v['id'],
                'uname' => $uname,
                'bg' => $v['bg'],
                'quata' => $v['quata'] / 100,
                'sceneName' => $sceneName,
                'full' => $v['full'] / 100,
                'reduce' => $v['reduce'] / 100,
                'expire' => $v['expire'] ? $v['expire'] : '-',
                'status' => $v['status'],
                'statusName' => $statusName,
                'applytime' => $v['addtime'] ? $v['addtime'] : '-',
                'usetime' => $v['usetime'] ? $v['usetime'] : '-',
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 会员劵模板列表
     * @param array $pages【start，limit】
     */
    public function tmpIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbUcouponTmp)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有模板');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //场景
            $scences = $this->scences;
            $sceneName = array_key_exists($v['scene'], $scences) ? $scences[$v['scene']] : '-';
            //类型
            $types = $this->types;
            $typeName = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'typeName' => $typeName,
                'bg' => $v['bg'],
                'quata' => $v['quata'] / 100,
                'sceneName' => $sceneName,
                'full' => $v['full'] / 100,
                'reduce' => $v['reduce'] / 100,
                'opentime' => $v['opentime'] ? $v['opentime'] : '随时可以',
                'expire' => $v['expire'],
                'addtime' => $v['addtime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 模板选项
     */
    public function tmpOption()
    {
        $types = $this->types;
        $scenes = $this->scences;
        $arys['type'] = [];
        $arys['scene'] = [];
        foreach ($types as $k => $v) {
            $arys['type'][] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        foreach ($scenes as $k => $v) {
            $arys['scene'][] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 模板添加
     */
    public function tmpAdd($pas)
    {
        $pas['quata'] = $pas['quata'] * 100;
        $pas['full'] = $pas['full'] * 100;
        $pas['reduce'] = $pas['reduce'] * 100;
        $pas['addtime'] = toDate();
        $lastid = DB::table($this->dbUcouponTmp)->insertGetId($pas);
        if (!$lastid) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 模板删除
     */
    public function tmpDel($id)
    {
        $res = (array) DB::table($this->dbUcouponTmp)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '模板不存在');
        }
        $pas['deltime'] = toDate();
        $up = DB::table($this->dbUcouponTmp)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}